#!/bin/bash
#
# Script gets info about users and groups from remote servers
# Ermakov Konstantin 01.04.2011
#
# $Id: get_users_info.sh 150 2011-04-06 17:00:59Z mamartynov@gmail.com $
#

. $OPENARS_HOME/common/debug.h || exit 1
#. $OPENARS_HOME/common/functions.h || exit 1

user=$USER_ARS
[ -z "$user" ] && user=$LOGNAME
[ -z "$1" ] && { ERR "need parameter \"filename\""; exit 1; }
[ ! -f "$1" ] && { ERR "not find file \"$1\""; exit 1; }

srvlist=`cat $1`
put_file="$PRJDIR/get_users_info.local.sh"
ssh_command="./get_users_info.local.sh; rm ./get_users_info.local.sh"

for host in $srvlist
do
    (
    sftp -o BatchMode=yes $user@$host &> /dev/null << END
put $put_file
END
    sftp_code=$?
    [ $sftp_code -ne 0 ] && { WARN "Error sftp-connect or sftp-commands on $host: exit_code=$sftp_code"; continue; }

    echo ' <host hostname="'$host'">' > $TEMPDIR/$host.xout
    ssh $user@$host "$ssh_command" >> $TEMPDIR/$host.xout
    echo ' </host>' >> $TEMPDIR/$host.xout
)
done

WAIT 180

echo '<?xml version="1.0" encoding="UTF-8"?>' > $OUTFILE
echo '<userinfo>' >> $OUTFILE

for ofile in `ls $TEMPDIR/*.xout`
do
    cat $ofile >> $OUTFILE
done

echo '</userinfo>' >> $OUTFILE

ECHO "$OUTFILE"

exit 0
